﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DQYT.HTDZ.BLL;
using System.Data;

namespace DQYT.HTDZ.Web.Views.SystemManage
{
    public partial class bdjtwellManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            #region
            try
            {
                BDMenuBLL menuBLL = new BDMenuBLL();
                lblPath.Text = menuBLL.GetTitlePath(Request["mnu_id"]);
                if (!IsPostBack)
                {
                    QueryData("");
                }
            }
            catch (Exception ex)
            {

            }
            #endregion
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="strName">井名</param>
        private void QueryData(string wellname)
        {
            DataTable dt = new DataTable();
            BDJTWELLBLL bdjtwell = new BDJTWELLBLL();
            dt = bdjtwell.GetList("TYJM like'%" + wellname + "%'").Tables[0];

            if (dt == null || dt.Rows.Count < 1)
            {
                dt.Rows.Add(dt.NewRow());
            }

            gvData.DataSource = dt;
            gvData.DataKeyNames = new string[] { "WELLID" };
            gvData.Columns[0].Visible = false;
            gvData.Columns[1].Visible = false;
            gvData.DataBind();
        }

        protected void GvData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //如果没有数据则添加一行“没有数据”行
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string sRow = e.Row.Cells[4].Text.Trim().ToString().Replace("&nbsp;", "");

                    if (string.IsNullOrEmpty(sRow))
                    {
                        return;
                    }
                    e.Row.Cells[3].Text = (e.Row.RowIndex + 1).ToString();

                    if (e.Row.Cells[13].Text == "1")
                    {
                        e.Row.Cells[13].Text = "正常";
                    }
                    else
                    {
                        e.Row.Cells[13].Text = "停用";
                    }

                    string strID = gvData.DataKeys[e.Row.RowIndex]["WELLID"].ToString();
                    string strID1 = Globals.CurrentUserInfo.USE_ID.ToString();// this.Session["id"].ToString();
                    if (strID == strID1)
                    {
                        CheckBox ck = e.Row.Cells[17].FindControl("ckFlag") as CheckBox;
                        ck.Enabled = false;
                    }

                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message.ToString());
            }

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {

            Response.Redirect("Bdjtwell.aspx");

        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDel_Click(object sender, EventArgs e)
        {
            #region

            try
            {
                int n = 0;
                if (this.gvData.Rows.Count > 0)
                {
                    BDJTWELLBLL jtwell = new BDJTWELLBLL();
                    for (int i = 0; i < gvData.Rows.Count; i++)
                    {
                        CheckBox ck = gvData.Rows[i].Cells[13].FindControl("ckFlag") as CheckBox;
                        if (ck.Checked == true && ck.Enabled == true)
                        {
                            ++n;
                            string strID = gvData.DataKeys[i]["WELLID"].ToString();
                            jtwell.Delete(strID);
                        }

                    }
                    if (n == 0)
                    {
                        ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                    }
                    else
                    {
                        QueryData(txtName.Text.Trim());
                        lblMsg.Text = "成功删除 " + n.ToString() + " 个井！";
                    }

                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = "错误信息：" + ex.Message;
            }

            #endregion
        }
        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            #region

            try
            {
                if (!string.IsNullOrEmpty(txtName.Text))
                {
                    QueryData(txtName.Text.Trim());
                }
                else
                {
                    QueryData("");
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = "错误信息：" + ex.Message;
            }

            #endregion
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lnkbtnEdit_Click(object sender, EventArgs e)
        {
            #region
            string id = (sender as LinkButton).CommandArgument;
            Response.Redirect("Bdjtwell.aspx?id=" + id + "&action=update");
            #endregion
        }

        protected void gvData_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvData.PageIndex = e.NewPageIndex;
            QueryData("");
        }
    }
}